<?php
namespace Application\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Predicate;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class SubscriberTable
{
    protected $tableGateway;
	protected $select;
	
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addsubscriber($subscriberEmailId)
    {
		$data = array(
		'subscriber_email' => $subscriberEmailId,  	
		'status'  	=> 1,
		);	
		$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;
    }
	public function getallsubscribers()
	{
		$select = $this->tableGateway->getSql()->select()
		->where(array('status' => 1));	
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}	
	public function unsubscribeuser($subscriber_email)
    {
		$result=$this->tableGateway->update(array('status'=>'0'),array('subscriber_email'=>$subscriber_email));
		return  $result;
    }
	public function getsubscriberlistGrid($searchkey,$aColumns,$data,$paginated=false)
	{
		$select = $this->tableGateway->getSql()->select();
		// Search
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('subscriber_email', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		// End
		
		// Sort Order
		if(isset($data['iSortCol_0']))
		{
			$sOrder="";
			for($i=0;$i<intval($data['iSortingCols']);$i++)
			{
				if($data['bSortable_'.intval($data['iSortCol_'.$i])] == "true")
				{
					$sOrder.=$aColumns[intval($data['iSortCol_'.$i])]." ".$data['sSortDir_'.$i].",";
				}
			}
			$sOrder = substr_replace($sOrder,"", -1);
		}
		if($sOrder!=''){
			$select->order($sOrder);
		}
		// End
		
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			//echo "<pre>";print_r($data['iDisplayLength']);exit;
			$select->order('subscriber_id DESC')->limit($currentPage)->offset($start);
		}
		// End
		
		$resultSet = $this->tableGateway->selectWith($select);
		 return $resultSet;
	}
	public function changeStatus($subIds,$value)
	{
		$data = array(
			'status'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(subscriber_id IN ('.$subIds.'))'));
		return 	$updateStatus;			
	}
	public function getcount($data='')
	{
		$select = $this->tableGateway->getSql()->select();
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('subscriber_email', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->count();
	}
	public function subscribExists($subscribeEmail)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->where('subscriber_email="'.$subscribeEmail.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();		
	}	
	public function deletesubscriberId($ids){		
		$this->tableGateway->delete(array('(subscriber_id IN ('.$ids.'))'));			
		return $this->tableGateway->lastInsertValue;	
	}
	public function subscriberStatus($userEmail,$status)
    {
		$data = array(		 	
		'status'  	=> $status,
		);	
		$this->tableGateway->update($data, array('subscriber_email' => $userEmail));
		return $this->tableGateway->lastInsertValue;
    }
}